package com.lm.activiti.boot.datatype;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.junit.Test;

import java.util.Iterator;
import java.util.Map;
import java.util.Stack;

/**
 * @author lm_jcf
 * @date 2020/4/16 11:10
 */
public class MapTest {


    @Test
    public void concurrentMap(){

        // 弱一致迭代方式
//        Map<String, Integer> map = Maps.newHashMap();
        Map<String, Integer> map = Maps.newConcurrentMap();

        map.put("de", 2);

        Iterator<String> iterator = map.keySet().iterator();

        map.remove("de");

        System.out.println(map);

        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);
        }

    }

    @Test
    public void findMax(){
        String className = "高二年级1班";

        System.out.println(className.substring(className.lastIndexOf("年级") + 2));

        ImmutableMap<String, Integer> map = ImmutableMap.of("A", 111, "B", 100, "C", 33);



        for (String userStandardScore : map.keySet()) {
            if (map.get(userStandardScore).equals(map.values().stream().mapToInt(Integer::valueOf).summaryStatistics().getMax())) {

                System.out.println(userStandardScore);
            }
        }

    }


    @Test
    public void toHex(){
        String hexStr = "02 35 44 33 42 30 30 35 30 0D 0A 03";

        for (String s : hexStr.split(" ")) {
//            System.out.println(hexToDec(s));
        }

        System.out.println((char) 46);
    }

    public  int hexToDec(String s) {
        String s1 = s.toUpperCase(); // 全转大写
        char[] chars = s1.toCharArray(); // 转成 char 数组
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < chars.length; i++) {
            stack.push(chars[i]); // 放入栈中，倒序遍历
        }
        int sum = 0;  // 定义总和
        int size = stack.size(); // 要先赋值给 size ，不然 stack.pop() 之后 size 会变
        for (int i = 0; i < size; i++) {
            Character pop = stack.pop();
            if (String.valueOf(pop).matches("[A-F]")) {  // 如果是 A-F
                sum += (Math.pow(16, i) * ((pop - 55))); // A的ASCII码为 65，取偏移量
            } else { // 如果是纯数字
                sum += Math.pow(16, i) * Integer.parseInt(String.valueOf(pop));
            }
        }
        return sum;
    }

}
